//
// Created by miaomiaole on 2024/1/21.
//
#include <iostream>
#include <opencv2/opencv.hpp>
#include "opencv2/imgcodecs.hpp"

using namespace std;
using namespace cv;

void colorReduce(Mat &image, int div = 64);

void colorReduce(Mat &image, int div) {
    for (int i = 0; i < image.rows; ++i) {
        for (int j = 0; j < image.cols; ++j) {
            image.at<Vec3b>(i, j)[0] = image.at<Vec3b>(i, j)[0] + div * 2;
            image.at<Vec3b>(i, j)[1] = image.at<Vec3b>(i, j)[1] + div * 2;
            image.at<Vec3b>(i, j)[2] = image.at<Vec3b>(i, j)[2] + div / 2;
        }
    }
    imshow("cat", image);
    waitKey(0);
}

int main() {
    Mat r1; //构造无参数矩阵
    //构造两行两列，深度是8位比特的单通道矩阵
    Mat r2(2, 2, CV_8UC1);

    // 构造行数是2，列数是3*通道数，深度是8位比特的3通道矩阵
    Mat r3(Size(3, 2), CV_8UC3);

    Mat r4(4, 4, CV_8UC2, Scalar(1, 3));

    //构造5行3列像素矩阵，深度是8位比特的3通道矩阵，且每个像素的通道初始值是4,5,6
    Mat r5(Size(3, 5), CV_8UC3, Scalar(4, 5, 6));

    // 赋值构造（指向同一个地址）
    Mat r6(r5);

    int sz[2] = {3, 3};

    Mat r7(2, sz, CV_8UC1, cv::Scalar::all(1));


    int a[2][3] = {1, 2, 3, 4, 5, 6};
    Mat r8(2, 3, CV_32S, a);

    cout << r1 << endl << r2 << endl << r3 << endl << r4 << endl << r5 << endl << r6 << endl << r7 << endl << r8;


    auto zeros = Mat::zeros(Size(300, 400), CV_8UC1);

    imshow("r5", r5);
    imshow("zeros", zeros);

    waitKey(0);

    Mat Image1 = imread("../520.jpg");
    Mat Image2 = imread("../2.jpg");


    // 矩形拷贝
    Mat imagedst = Image1(Rect(0, 0, Image2.cols, Image2.rows));


    imshow("imagedst", imagedst);

    waitKey(0);


/*    // 遍历Mat，使用指针方式效率高
    auto p_imagedst = imagedst.data;
    for (int i = 0; i < imagedst.rows; ++i) {
        for (int j = 0; j < imagedst.cols * imagedst.channels(); j++) {
            cout << (int) p_imagedst[j] << "   ";
        }
        cout << endl;
    }


    // 使用Mat的模板函数，三通道数组
    for (int i = 0; i < imagedst.rows; ++i) {
        auto pdata = imagedst.ptr(i);

        for (int j = 0; j < imagedst.cols * imagedst.channels(); ++j) {
            cout << (int) pdata[j] << "  ";
        }
        cout << endl;
    }*/

    // 使用 at(x,y)方式遍历
    auto cat = imread("../picture_1663037518439.jpeg");
    colorReduce(cat);


    auto begin = cat.begin<Vec3b>();
    auto end = cat.end<Vec3b>();
    while (begin != end) {

        // 单个像素
        auto vecArr = (*begin);

        // 像素
        // std::cout << "vecArr" << vecArr << std::endl;
        begin++;
    }

    // 当前像素
    cout << "cat pixel " << cat.total() << endl;


}

